package com.andreas.controller;

import com.andreas.exception.BussinessException;
import com.andreas.pojo.Evaluation;
import com.andreas.pojo.Member;
import com.andreas.pojo.MemberReadState;
import com.andreas.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author andreaszhou
 * @ClassName MemberController
 * @Description TODO
 * @date 2021/1/21 9:19
 * @Version 1.0
 */
@Controller
public class MemberController {
    @Resource
    private MemberService memberServiceImp;


    @GetMapping("/reg.html")
    public ModelAndView showReg() {
        return new ModelAndView("/register");
    }

    @GetMapping("/login.html")
    public ModelAndView showLogin() {
        return new ModelAndView("/login");
    }

    @PostMapping("/reg")
    @ResponseBody
    public Map register(String username, String password, String verifyCode, String nickname, HttpServletRequest request) {
        Map map = new HashMap();
        String kaptchaVerifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");
        if (kaptchaVerifyCode == null || verifyCode == null || !(kaptchaVerifyCode.equalsIgnoreCase(verifyCode))) {
            map.put("code", "VC01");
            map.put("msg", "验证码错误");
        } else {
            try {
                Member member = memberServiceImp.addMember(username, password, nickname, verifyCode);
                map.put("code", "0");
                map.put("msg", "success");
            } catch (BussinessException e) {
                e.printStackTrace();
                map.put("code", e.getCode());
                map.put("msg", e.getMsg());

            }
        }
        return map;
    }

    @PostMapping("/login")
    @ResponseBody
    public Map checkLogin(String username, String password, String verifyCode, HttpServletRequest request) {
        Map map = new HashMap();
        String kaptchaVerifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");
        if (kaptchaVerifyCode == null || verifyCode == null || !(kaptchaVerifyCode.equalsIgnoreCase(verifyCode))) {
            map.put("code", "VC01");
            map.put("msg", "验证码错误");
        } else {
            try {
                Member member = memberServiceImp.checkLogin(username, password, verifyCode);
                request.getSession().setAttribute("member",member);
                map.put("code", "0");
                map.put("msg", "success");
            } catch (BussinessException e) {
                e.printStackTrace();
                map.put("code", e.getCode());
                map.put("msg", e.getMsg());
            }
        }
        return map;
    }

    @PostMapping("/updateReadState")
    @ResponseBody
    public Map updateReadState(int memberId, int bookId,int readState){
        Map map = new HashMap();
        try{
            MemberReadState memberReadState = memberServiceImp.updateMemberReadState(memberId, bookId, readState);
            map.put("code","0");
            map.put("msg","success");
        }catch (BussinessException e){
            e.printStackTrace();
            map.put("code",e.getCode());
            map.put("msg",e.getMsg());
        }
        return map;
    }


    @PostMapping("/evaluation")
    @ResponseBody
    public Map addEvaluation(int memberId,int bookId,int score,String content){
        Map map = new HashMap();
        try{
            Evaluation evaluation = memberServiceImp.addEvaluation(memberId, bookId, score, content);
            map.put("code",0);
            map.put("msg","success");
            map.put("evaluation",evaluation);
        }catch(BussinessException e){
            e.printStackTrace();
            map.put("code",e.getCode());
            map.put("msg",e.getMsg());
        }
        return map;
    }


    @PostMapping("enjoy")
    @ResponseBody
    public Map addEnjoy(int evaluationId){
        Map map = new HashMap();
        try{
            Evaluation evaluation = memberServiceImp.addEnjoy(evaluationId);
            map.put("code","0");
            map.put("msg","success");
            map.put("evaluation",evaluation);
        }catch (BussinessException e){
            e.printStackTrace();
            map.put("code",e.getCode());
            map.put("msg",e.getMsg());
        }
        return map;
    }
}
